package org.example.server.tcp;

import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetSocket;

/**
 * @description TODO
 * @author sats@jz
 * @date 2025年01月05日 22:31
 */
public class VertxTcpClientTest {
    public void start(){

        // 创建Vert.x实例
        Vertx vertx = Vertx.vertx();
        vertx.createNetClient().connect(8088, "localhost", res -> {
            if (res.succeeded()) {
                System.out.println("Connected to TCP server");
                NetSocket socket = res.result();

                for (int i = 0; i < 1000; i++) {
                    Buffer buffer = Buffer.buffer();
                    String str = "Hello server!Hello, Server!Hello server!Hello, Server!";
//                    socket.write(str);
                    buffer.appendInt(0);
                    buffer.appendInt(str.getBytes().length);
                    buffer.appendBytes(str.getBytes());
                    socket.write(buffer);
                }

//                socket.write("Hello, TCP server!");
                socket.handler(buffer -> {
                    System.out.println("Received from TCP server: " + buffer.toString());
                });
            }else {
                System.out.println("Failed to connect to TCP server: ");
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpClientTest().start();
    }
}
